package design.util;

import java.util.*;

/**
 * @author: hero生仔
 * 题目选项乱序操作工具包
 */
public class TopicRandomUtil {
    /**
     * 乱序map元素，记录对应的答案key
     *
     * @param option 选项 A. **** B.**** ....
     * @param key    答案
     * @return Topic 乱序后 {A = c.，B = d.，C = a.，D = b.}
     */
    static public Topic random(Map<String, String> option, String key) {
        // 获取某一个题目的所有选项
        Set<String> keySet = option.keySet();
        // 对选项进行打乱顺序
        ArrayList<String> keyList = new ArrayList<>(keySet);
        Collections.shuffle(keyList);

        HashMap<String, String> optionNew = new HashMap<>();
        int idx = 0;
        String keyNew = "";
        for (String str : keySet) {
            // 第 idx+1 选项
            String random = keyList.get(idx++);
            if (key.equals(str)) {
                keyNew = random;
            }
            optionNew.put(random, option.get(str));
        }
        return new Topic(optionNew,keyNew);
    }
}
